// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.designer.ui.process.figures;

import org.dengues.commons.ui.gef.SimpleHtmlFigure;
import org.dengues.designer.ui.process.models.CompNode;
import org.eclipse.draw2d.Figure;
import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.ImageFigure;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.RGB;

/**
 * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * $Id: Dengues.epf 1 2006-09-29 17:06:40Z qiang.zhang $
 * 
 */
public class CompNodeFigure extends Figure {

    public final Color START_COLOR = new Color(null, new RGB(0xB0, 0xE7, 0));

    private final ImageFigure fig;

    private final ImageDescriptor im;

    private final SimpleHtmlFigure hint;

    /**
     * Qiang.Zhang.Adolf@gmail.com CompNodeFigure constructor comment.
     * 
     * @param imageDescriptor
     */
    public CompNodeFigure(ImageDescriptor imageDescriptor) {
        fig = new ImageFigure();
        im = imageDescriptor;
        fig.setImage(im.createImage());
        add(fig);
        this.setSize(getPreferredSize());
        this.setOpaque(true);
        hint = new SimpleHtmlFigure();
    }

    public void setHint(String hintText) {
        if (hintText.equals("")) { //$NON-NLS-1$
            setToolTip(null);
        } else {
            hint.setText(hintText);
            setToolTip(hint);
        }
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com CompNodeFigure constructor comment.
     * 
     * @param castModel
     */
    public CompNodeFigure(CompNode castModel) {
        this(castModel.getComponent().getIcon32());
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "getNodeSize".
     * 
     * @return
     */
    public Dimension getNodeSize() {
        Dimension size = new Dimension();
        size.height = im.getImageData().height;
        size.width = im.getImageData().width;
        return size;
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "getImageDescriptor".
     * 
     * @return
     */
    public ImageDescriptor getImageDescriptor() {
        return im;
    }

    @Override
    public void setBounds(final Rectangle rect) {
        super.setBounds(rect);
        this.fig.setBounds(rect);
    }

    @Override
    public void paint(Graphics graphics) {
        graphics.setAntialias(SWT.ON);
        super.paint(graphics);
    }

    public void setStart(boolean start) {
        if (start) {
            setBackgroundColor(START_COLOR);
            setOpaque(true);
        } else {
            setOpaque(false);
        }
    }
}
